package com.clx;

import com.clx.utils.zookeeper.ZookeeperNode;
import com.clx.utils.zookeeper.ZookeeperUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;

import java.util.List;

/**
 * 注册中心的管理页面
 * */

@Slf4j
public class Application {
    public static void main(String[] args) {
        //帮我们创建基础目录
        //crpc - metadata（持久节点）
        //   └─ providers（持久节点）
        //        └─ service1（持久节点，接口的全限定名）
        //                  ├─ node1 [data] /ip:port
        //                  ├─ node2 [data]
        //                  └─ node3 [data]
        //   └─ consumers
        //          └─ service1
        //                  ├─ node1 [data]
        //                  ├─ node2 [data]
        //                  └─ node3 [data]
        //└─ config
        try {
            //创建一个zookeeper实例
            ZooKeeper zooKeeper = ZookeeperUtils.createZookeeper();
            // 定义节点和数据
            String basePath = "/ydlclass-metadata";

            String consumersPath = basePath + "/consumers";
            String providePath = basePath + "/providers";

            ZookeeperNode baseNode = new ZookeeperNode(basePath,null);
            ZookeeperNode consumersNode = new ZookeeperNode(consumersPath,null);
            ZookeeperNode provideNode = new ZookeeperNode(providePath,null);

            //创建节点
            List.of(baseNode,provideNode,consumersNode).forEach(node -> {
               ZookeeperUtils.createNode(zooKeeper,node,null,CreateMode.PERSISTENT);
            });

            ZookeeperUtils.close(zooKeeper);



        } catch (Exception e) {
            log.error("创建基础目录时，产生异常，如下",e);
        }
    }
}